<?php
/**
 * Created by PhpStorm.
 * User: Webdd
 * Date: 15/4/28
 * Time: 下午3:20
 */
namespace Android\Model;

class StudentModel extends BaseModel {

    public function login ($student_id, $password) {
        $map['student_id'] = $student_id;
        $map['password'] = md5($password);
        $student = $this->where($map)->find();
        if (empty($student)) {
            return false;
        }
        return $student;
    }

    public function isExist($student_id) {
        $student = $this->where("student_id = '{$student_id}'")->find();
        if (empty($student)) {
            return false;
        }
        return true;
    }



    public function getStudentByIdsStr ($str) {

        if (strlen(trim($str)) == 0) return null;
        $sidArr = explode(',',$str);

        $count = 0;
        $stuArr = array();
        foreach ($sidArr as $v) {
            if ($count == 5) break;
            $stu = $this->where('id = '.$v)->getField('id,name,photo');
            if (empty($stu)) continue;
            $stuArr[] = $stu[$v];
            $count ++ ;
        }
        return $stuArr;
    }



    /**
     * 根据学生ID数组,获取所有学生
     *
     * @param $idArr
     * @return array
     */
    public function getStudentArrByIdArr($idArr) {
        $studentArr = array();
        foreach ($idArr as $v) {
            $s = $this->find($v);
            if (!empty($s)) $studentArr[] = $s;
        }

        return $studentArr;
    }


    /**
     * 根据班级ID，获取该班级的学生人数
     *
     * @param $class_id
     */
    public function getScountByCid ($class_id) {
        return $this->where('class_id = '.$class_id)->count();
    }

    /**
     * 根据班级ID，获取该班级的所有学生
     *
     * @param $class_id
     */
    public function getStudentByCid($class_id , $field = '*') {
        $map['class_id'] = $class_id;
        $result = $this->field($field)->where($map)->select();
        if (empty($result))
            return null;

        return $result;
    }



    /**
     * 更具ID获取指定学生
     *
     * @param $id
     * @param string $field
     * @return mixed
     */
    public function getStudentById($id , $field = '*') {
        return  $this->field($field)->find($id);
    }


}